package javatest;

import framework.sepal4java.common.struct.ConstantEnum.CmdType;
import framework.sepal4java.common.struct.ConstantEnum.SensorMode;
import framework.sepal4java.common.struct.SensorData;
import framework.sepal4java.lib4app.AbstractApp;
import framework.sepal4java.lib4app.AppRemoteConnector;
import framework.sepal4java.lib4app.PlatformException;

public class AppDemo1 extends AbstractApp {
    @Override
    public void configApp() {
        this.appName = "AppDemo1";
        this.appDescription = "This is AppDemo1";
    }

    @Override
    public void getMsg(String sensorName, SensorData value) {
        //logger.info(String.format("[%s]: getMsg(sensorName, value) -> %s, %s", appName, sensorName, value));
    }

    public static void main(String[] args) {
        try {
            AppDemo1 demo = new AppDemo1();
            AppRemoteConnector connector = AppRemoteConnector.getInstance();
            connector.connectPlatform("127.0.0.1", 9090);
            connector.registerApp(demo);

            // Register sensors and actors
            connector.registerSensor("Sensor1", SensorMode.PASSIVE, 1);
            connector.registerActor("Actor1");

            // Start service
            // CtxServiceConfig ctxServiceConfig = new CtxServiceConfig("rule_path", "pattern_path", "bfunc_path", null, null);
            // connector.serviceStart(ServiceType.CTX, ctxServiceConfig);

            // Start sensing
            connector.getMsgThread(CmdType.START);

            // Send command to actor when needed
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                connector.sendActorCmd("Actor1", "mock_cmd");
            }

            // Stop sensing
            connector.getMsgThread(CmdType.STOP);

            // Stop service
            // connector.serviceStop(ServiceType.CTX);

            // Unregister sensors and actors
            connector.cancelAllSensors();
            connector.cancelAllActors();

            connector.unregisterApp(demo);
            connector.disConnectPlatform();
        } catch (PlatformException e) {
            System.out.println("Failed to Connect！");
        }
    }
}
